import swal from 'sweetalert2';
import { Component } from '@angular/core';
import { LazyLoadEvent } from 'primeng/components/common/api';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';

import { User } from '../../../core/auth/user.service';
import { CommonService } from '../../../core/common/common.service';
import { TableDictService } from '../../../core/common/table-dict.service';
import { Code2NameService } from '../../../core/common/code-name.service';
import { ChannelService } from '../service/channel.service';
import { Observable } from 'rxjs';
@Component({
  selector: 'ncloud-channel-manage',
  templateUrl: './channelManage.html',
  styleUrls: ['./channelManage.scss']
})
export class ChannelManage {
  public customers: Array<any>;
  public totalRecords: number;
  public nodeInfos: Array<any> = [];
  public selectedChannel: any;
  constructor(private common: CommonService, public user: User, private channelService: ChannelService,
    private route: ActivatedRoute, private router: Router, private tableDict: TableDictService,
    private translate: TranslateService, private code2Name: Code2NameService) {
  }
  loadLazy(event: LazyLoadEvent) {
    setTimeout(() => {
      const pageNum = event.first / event.rows + 1;
      const pageSize = event.rows;
      this.channelService.getChannels(pageNum, pageSize).map(res => {
        this.tableDict.get('CHANNEL_TYPE', true).subscribe(data => {
          res.list.forEach((r: any) => {
            data.forEach((c: any) => {
              if (c.value === r.chnlType) {
                r.chnlTypeName = c.label;
              }
            });
            r.address = r.address ? r.address : '';
          });
        });
        return res;
      }).map(res => {
        res.list.forEach((r: any) => {
          this.code2Name.getCountyName(r.province).subscribe(p => {
            r.provinceName = p;
          });
        });
        return res;
      }).map(res => {
        res.list.forEach((r: any) => {
          this.code2Name.getCountyName(r.area).subscribe(p => {
            r.areaName = p;
          });
        });
        return res;
      }).subscribe((res: any) => {
        this.customers = res.list;
        this.totalRecords = res.total;
      });
    }, 200);
  }
  public addChannel(): void {
    this.jump('/channel/addChannel', '0');
  }
  public editChannel(): void {
    const url = '/channel/editChannel';
    this.jump(url, '1');
  }
  public quato(): void {
    const url = '/channel/quato';
    this.jump(url, '2');
  }
  private jump(url: string, operate: string): void {
    const params: any = { 'operate': operate, 'backURL': this.common.getCurrentURL(this.route) };
    if (operate === '1' || operate === '2') {
      if (this.selectedChannel) {
        params.data = JSON.stringify(this.selectedChannel);
      } else {
        this.translate.get(['channel.add.emptyWaringText', 'channel.add.warning']).subscribe((res: string) => {
          swal(res['channel.add.warning'], res['channel.add.emptyWaringText'], 'warning');
        });
        return;
      }
    }
    this.router.navigate([url, params], { relativeTo: this.route.parent, skipLocationChange: true });
  }
  onRowSelect(event) {
    //      console.log(JSON.stringify(event));
  }
  userManage(param): void {
    this.router.navigate(['/channel/userManage', {
      'chnlId': param.chnlId
    }], { relativeTo: this.route.parent, skipLocationChange: true });
  }
}
